package cn.bytengine.stem.beans;

import java.beans.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public abstract class Structs {
    private static final Map<Class<?>, BeanInfo> CACHE_BEAN_DESCRIPTORS = new ConcurrentHashMap<>();

    private Structs() {
    }

    public static BeanInfo getBeanDescriptor(Class<?> beanClass) throws IntrospectionException {
        BeanInfo beanInfo = CACHE_BEAN_DESCRIPTORS.get(beanClass);
        if (beanInfo == null) {
            synchronized (beanClass) {
                beanInfo = CACHE_BEAN_DESCRIPTORS.get(beanClass);
                if (beanInfo == null) {
                    beanInfo = Introspector.getBeanInfo(beanClass);
                    CACHE_BEAN_DESCRIPTORS.put(beanClass, beanInfo);
                }
            }
        }
        return beanInfo;
    }

    public static PropertyDescriptor[] getProperties(Class<?> beanClass) throws IntrospectionException {
        BeanInfo beanInfo = getBeanDescriptor(beanClass);
        return beanInfo.getPropertyDescriptors();
    }
}
